#include "musicslider.h"
#include "ui_musicslider.h"
#include<QMouseEvent>
#include<QDebug>
MusicSlider::MusicSlider(QWidget *parent):
    QWidget(parent),
    ui(new Ui::MusicSlider)
{
    ui->setupUi(this);
    maxWidth=this->width();
    currentPos=0;
    moveSilde();
    //qDebug() << ui->inline_2->geometry();
}

MusicSlider::~MusicSlider()
{
    delete ui;
}

void MusicSlider::setStep(float ratio)
{
    currentPos=maxWidth*ratio;
    moveSilde();
}

void MusicSlider::mouseMoveEvent(QMouseEvent *event)//拖拽
{
   // QRect musicsliderRect=QRect(0,0,geometry.width(),geometry.height());
    //if(!musicsliderRect.contains(event->pos()))//判断鼠标是否在矩形内部点击，如果不是，就不能拖拽。
    //{
    //    return;
    //}
    if(event->buttons()==Qt::LeftButton)
    {
         currentPos=event->pos().x();
        if(currentPos<0)
        {
         currentPos=0;
        }
        if(currentPos>maxWidth)
        {
           currentPos=maxWidth;
        }
        qDebug()<<"拖拽"<<maxWidth;
         moveSilde();
    }
}

void MusicSlider::mousePressEvent(QMouseEvent *event)//按下
{
    currentPos=event->pos().x();
    qDebug()<<"按下";
    moveSilde();
    emit setMusicSliderPosition(ui->outline->width()/(float)maxWidth);
}

void MusicSlider::mouseReleaseEvent(QMouseEvent *event)//抬起
{
    currentPos=event->pos().x();
    qDebug()<<"抬起";
    moveSilde();
}

void MusicSlider::moveSilde()
{
    ui->outline->setGeometry(ui->outline->x(),ui->outline->y(),currentPos,ui->outline->height());
}
